package config;

import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.ComponentScan;
import org.springframework.context.annotation.Configuration;
import org.springframework.http.MediaType;
import org.springframework.http.converter.HttpMessageConverter;
import org.springframework.http.converter.json.MappingJackson2HttpMessageConverter;
import org.springframework.web.servlet.config.annotation.EnableWebMvc;
import org.springframework.web.servlet.config.annotation.WebMvcConfigurerAdapter;
import org.springframework.web.servlet.view.InternalResourceViewResolver;

import java.nio.charset.Charset;
import java.util.Arrays;
import java.util.List;

@EnableWebMvc
@Configuration
@ComponentScan("com.*")//指定扫描包，包下面带有特殊注解的类都被spring初始化
public class SpringConfiguration extends WebMvcConfigurerAdapter {
    @Override
    public void extendMessageConverters(List<HttpMessageConverter<?>> messageConverters) {
        MappingJackson2HttpMessageConverter converter = new MappingJackson2HttpMessageConverter();
        MediaType[] supportedMediaTypes = { new MediaType("text", "html", Charset.forName("utf-8")) };

        converter.setSupportedMediaTypes(Arrays.asList(supportedMediaTypes));
//        for (MediaType mediaType : converter.getSupportedMediaTypes()) {
//            System.out.println(mediaType.getType() + "-----" + mediaType.getSubtype());
//        }
        messageConverters.set(6, converter);//为什么这个参数是6
//        int count = 0;
//        for (HttpMessageConverter httpMessageConverter : messageConverters) {
//            System.out.println(httpMessageConverter.getClass().getSimpleName() + "  --  " +count);
//            List<MediaType> mediaTypes = httpMessageConverter.getSupportedMediaTypes();
//            for (MediaType mediaType : mediaTypes) {
//                System.out.println(mediaType.getType() + " " + mediaType.getSubtype());
//            }
//            count++;
//        }
    }
    @Bean
    public InternalResourceViewResolver getInternalResourceViewResolver(){
        InternalResourceViewResolver internalResourceViewResolver = new InternalResourceViewResolver();
        internalResourceViewResolver.setPrefix("/WEB-INF/jsp");
        internalResourceViewResolver.setSuffix(".jsp");
        return internalResourceViewResolver;
    }
}
